﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace FileRenamer
{
	public partial class rules : Form
	{
		public rule Rule;

		public rules(main win)
		{
			InitializeComponent();
			this.Load += new EventHandler(rules_Load);

			this.Rule = win.Rule;

			Properties.Settings setting = Properties.Settings.Default;
			c_FromRegex.Items.AddRange(setting.RegFrom.Trim().Split('\n'));
			c_ToRegex.Items.AddRange(setting.RegTo.Trim().Split('\n'));
			c_Userdef.Items.AddRange(setting.UserDef.Trim().Split('\n'));
		}

		void rules_Load(object sender, EventArgs e)
		{
			if (Rule.Method == rules.Methods.Regex)
			{
				r_Regex.Checked = true;
				c_FromRegex.Text = Rule.From;
				c_ToRegex.Text = Rule.To;
			}
			else
			{
				r_Userdef.Checked = true;
				c_Userdef.Text = Rule.From;
			}
		}

		private void lnk_Help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{

		}

		public enum Methods { Regex, Userdef }
		public struct rule
		{
			public Methods Method { get; set; }
			public string From { get; set; }
			public string To { get; set; }
		}

		private void b_OK_Click(object sender, EventArgs e)
		{
			if (r_Regex.Checked)
			{
				Rule = new rule { Method = Methods.Regex, From = c_FromRegex.Text, To = c_ToRegex.Text };
			}
			else
			{
				Rule = new rule { Method = Methods.Userdef, From = c_Userdef.Text, To = string.Empty };
			}
		}

		private void c_Userdef_Enter(object sender, EventArgs e)
		{
			r_Userdef.Checked = true;
		}

		private void c_FromRegex_Enter(object sender, EventArgs e)
		{
			r_Regex.Checked = true;
		}

		private void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			int p = c_Userdef.SelectionStart;
			string data = e.ClickedItem.Tag.ToString(), os = c_Userdef.Text;
			c_Userdef.Text = os.Substring(0, p) + data + os.Substring(p);
			c_Userdef.Focus();
			c_Userdef.SelectionStart = p;
			c_Userdef.SelectionLength = data.Length;
		}
	}
}
